package com.pig.jjfetion.core.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.pig.jjfetion.core.FetionClient;
import com.pig.jjfetion.core.enumx.AccountTypeEnum;
import com.pig.jjfetion.core.utils.XMLHelper;

public class LocaleSetting {

	/*
	 * 配置的Dom对象
	 */
	private Document document;
	/*
	 * 配置是否已经加载
	 */
	private boolean isLoaded;
	/*
	 * 默认构造函数,初使化对象 
	 */
	public LocaleSetting(){
		this.isLoaded = false;
		this.document = null;
	}
	
	/*
	 * 加载自适应配置
	 * @param accountType
	 * @param accountName
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 */
	public void load(AccountTypeEnum accountTypeEnum, String accountName) throws IOException, JDOMException{

		URL url = new URL(FetionConfig.getString("server.nav-system-uri"));
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.addRequestProperty("User-Agent", "IIC2.0/PC"+FetionClient.PROTOCOL_VERSION);
		
		String accountType = null;
		switch(accountTypeEnum){
		    case EMAIL:
		    	accountType = "email=\""+accountName+"\"";
		    	break;
		    case MOBILE:
		    	accountType = "mobile-no=\""+accountName+"\"";
		    	break;
		    case FETION:
		    	accountType = "sid=\""+accountName+"\"";
		    	break;
		    default:
		    	throw new IllegalStateException("Invalid CMCC mobile number, FetionId or Registered Email.");
		}
		
		StringBuilder content = new StringBuilder();
		content.append("<config><user ");
		content.append(accountType);
		content.append(" /><client type=\"PC\" version=\"");
		content.append(FetionClient.PROTOCOL_VERSION);
		content.append("\" platform=\"W5.1\" />");
		content.append("<servers version=\"0\" />");
		content.append("<service-no version=\"0\" />");
		content.append("<parameters version=\"0\" />");
		content.append("<hints version=\"0\" />");
		content.append("<http-applications version=\"0\" />");
		content.append("<client-config version=\"0\" />");
		content.append("<services version=\"0\" />");
		content.append("</config>");
		
		OutputStream out = conn.getOutputStream();
		out.write(content.toString().getBytes());
		out.flush();
		
		SAXBuilder builder = new SAXBuilder();
		this.document = builder.build(conn.getInputStream());
		this.isLoaded = true;
	}
	
	/*
	 * 返回一个节点的值
	 */
	public String getNodeText(String path){
		if(this.document != null){
			Element el = XMLHelper.find(this.document.getRootElement(), path);
			return el !=null?el.getText():null;
		}else{
			return null;
		}
	}

	/*
	 * 检查是否是有效配置，任意取配置中的一个节点，如果有值有明有效
	 */
	public boolean isValid(){
		return this.getNodeText("/config/servers/sipc-proxy")!=null;
	}	
	
	/*
	 * 从文件输入流中加载配置
	 */
	public void load(InputStream in) throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();
		this.document = builder.build(in);
		this.isLoaded = true;
	}
	/*
	 * 是否被加载
	 * @return isLoaded
	 */
	public boolean isLoaded() {
		return isLoaded;
	}
}
